/*
 * Copyright (c) 2017, dhl and/or its affiliates. All rights reserved.
 * dhl PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 */
package com.xd.cloud.service.core;

import java.util.List;

import com.xd.cloud.common.Constants;
import com.xd.cloud.dao.core.RoleDao;
import com.xd.cloud.entity.core.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xd.cloud.dao.Page;
import com.xd.cloud.dao.core.OrgUserDao;
import com.xd.cloud.dao.core.UserDao;
import com.xd.cloud.dao.core.UserRoleDao;
import com.xd.cloud.entity.core.OrgUser;
import com.xd.cloud.entity.core.User;
import com.xd.cloud.entity.core.UserRole;


/**
 * 用户service
 * 
 * @author dhl
 * @since  V1.0
 * 
 */
@Service
public class UserService {

	@Autowired
	private UserDao userDao;
	@Autowired
	private UserRoleDao userRoleDao;
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private OrgUserDao orgUserDao;
	
	/**
	 * 根据用户名查询用户角色信息
	 * @param username
	 * @return 角色rolekey的列表
	 */
	public List<Object> findUserRole(String username)
	{
		return userRoleDao.findUserRole(username);
	}
		
	
	public List<UserRole> getUserRole(String username)
	{
		User user = userDao.getByUserAccount(username);
		return userRoleDao.getByuserid(user.getId());
	}
	
	public User get(int id)
	{
		return userDao.get(id);
	}
	
//	public List<UserRole> getAllTeacher(String roleName) {
//		Role role = roleDao.getByRoleName(roleName);
//		return userRoleDao.getByRoleId(role.getId());
//	}
//	
//	public List<User> getStudentBySchoolName(String roleName,String schoolname) {
//		
//		Role role = roleDao.getByRoleName(roleName);
//		List<UserRole> list = userRoleDao.getByRoleId(role.getId());
//		List<User> userList = new ArrayList();
//		return userList;
//	}
	
	public boolean remove(int id)
	{
		userRoleDao.removeByuserid(id);
		User c = userDao.get(id);
		if (c != null)
		{
			userDao.delete(c);
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public boolean update(User c)
	{
		userDao.update(c);
		return true;
	}
	
	public boolean updateUser(User user,int orgid) {
		
		userDao.update(user);
		
		OrgUser ou = orgUserDao.getByuserid(user.getId());
		if (ou != null && orgid == ou.getCompanyid())
		{
			return true;
		}
		if (ou == null)
		{
			ou = new OrgUser();
			ou.setUserid(user.getId().intValue());
			ou.setCompanyid(orgid);
			orgUserDao.save(ou);	
		}
		else
		{
			ou.setCompanyid(orgid);
			orgUserDao.update(ou);
		}
		return true;
	}

	public List<User> getAll()
	{
		return userDao.getAll();
	}
	public Page getAllUser(int pageNo,int pageSize)
	{
		return userDao.getAllUser(pageNo, pageSize);
	}

	public Page getAllUserBySessionUserId(int pageNo,int pageSize,int sessionUserId)
	{
		return userDao.getAllUserBySessionUserId(pageNo, pageSize,sessionUserId);
	}

	public User queryUserInformation(Integer id) {
		return userDao.get(id);
	}
	
	public List<User> searchUser(String name,int orgid)
	{
		return userDao.searchUser(name,orgid);
	}
	
	public List<User> search(String name)
	{
		return userDao.search(name);
	}
	
	public Page getUsersByOrgId(int orgid,int pageNo, int pageSize) {
		return userDao.getUsersByOrgId(orgid,pageNo,pageSize);
	}
	
	public boolean saveUser(User user,int orgid) {
		
		User u = getByUserAccount(user.getAccount());
		if (u != null)
		{
			return false;
		}
		userDao.save(user);
		//创建用户，系统默认角色
		Role role = roleDao.get(Constants.Role.COMMON_USER);
		UserRole userRole = new UserRole();
		userRole.setUserid(user.getId());
		userRole.setRole(role);
		userRoleDao.save(userRole);
		OrgUser ou = new OrgUser();
		ou.setCompanyid(orgid);
		ou.setUserid(user.getId());
		orgUserDao.save(ou);		
		return true;
	}
	
		
	public User getByUserAccount(String username) {
		return userDao.getByUserAccount(username);
	}
	
	public void remove(String ids) {
		userDao.remove(ids);
	}
	
	public void resetpsw(String ids,String psw) {
		userDao.resetpsw(ids, psw);
	}
	
	public void updateisenable(String ids,int isenable) {
		userDao.updateisenable(ids, isenable);
	}
	
	public Object getCount(int orgid,int roleid)
	{
    	return userDao.getCount(orgid, roleid);
	}
	
	public Object getCount(int roleid)
	{
    	return userDao.getCount(roleid);
	}
	
	public Object getCountNew(int roleids)
	{
		return userDao.getCountNew(roleids);
	}
	
	public Object getCountNewId(String roleids)
	{
		return userDao.getCountNewId(roleids);
	}
	
	
	public List<User> getByRolekey(int orgid,String rolekeys){
		return userDao.getByRolekey(orgid,rolekeys);
	}
	
	public Page getByParmars(int pageNo, int pageSize,int orgid,String joindate,int majorid)
	{
		return userDao.getByParmars(pageNo, pageSize, orgid, joindate, majorid);
	}
	
	public Page getByClassid(int pageNo,int pageSize,int classid){
		return userDao.getByClassid(pageNo, pageSize, classid);
	}
	
	public User getByUseridRolename(int userid,String rolekey){
		return userDao.getByUseridRolename(userid,rolekey);
	}

//	public User getUserByPhone(String phone) {
//		return userDao.getUserByPhone(phone);
//	}	

}

